export const searchTable = (data, content, type, sortKey) => {
  let result = [...data];
  let start = (content.page - 1) * content.size;
  let end = start + content.size;
  // 排序
  if (sortKey) {
    result.sort((a, b) => {
      return new Date(b[sortKey]).getTime() - new Date(a[sortKey]).getTime();
    });
  } else {
    result.sort((a, b) => {
      return Number(b.id) - Number(a.id);
    });
  }

  // 筛选
  Object.keys(content).forEach((key) => {
    let value = content[key];
    // 日期筛选
    if (key == "time" && Array.isArray(value)) {
      if (value.length > 0) {
        let searchStart = new Date(value[0]).getTime();
        let searchEnd = new Date(
          new Date(value[1]).getTime() + 24 * 60 * 60 * 1000
        ).getTime();
        result = result.filter((item) => {
          let start, end;
          if (type == 1) {
            start = new Date(item.start_time).getTime();
            end = new Date(item.end_time).getTime();
          } else if (type == 3) {
            start = new Date(item.create_time).getTime();
            end = new Date(item.create_time).getTime();
          } else {
            start = new Date(item.id).getTime();
            end = new Date(item.id).getTime();
          }
          return searchEnd >= end && start >= searchStart;
        });
      }
    }

    // 日期筛选
    if (key == "create_time" && Array.isArray(value)) {
      if (value.length > 0) {
        let searchStart = new Date(value[0]).getTime();
        let searchEnd = new Date(
          new Date(value[1]).getTime() + 24 * 60 * 60 * 1000
        ).getTime();
        result = result.filter((item) => {
          let start;
          if (type == 1) {
            start = new Date(item.create_time).getTime();
          } else {
            start = new Date(item.id).getTime();
          }
          return searchEnd >= start && start >= searchStart;
        });
      }
    }

    // 关键字筛选
    if (typeof value === "string" && value.length > 0) {
      result = result.filter((item) => {
        return item[key] && item[key].includes(value);
      });
    }
    // 关键字筛选
    let flag =
      typeof value === "number" &&
      value > 0 &&
      key != "total" &&
      key != "page" &&
      key != "size";
    if (flag) {
      if (key == "unincome") {
        result = result.filter((item) => {
          if (value == 1) {
            return Number(item.total_price || 0) <= Number(item.income || 0);
          } else if (value == 2) {
            return Number(item.total_price || 0) > Number(item.income || 0);
          }
        });
      } else if (key == "uninvoice") {
        result = result.filter((item) => {
          if (value == 1) {
            return Number(item.total_price || 0) <= Number(item.invoicing || 0);
          } else if (value == 2) {
            return Number(item.total_price || 0) > Number(item.invoicing || 0);
          }
        });
      } else if (key == "outstanding_wages") {
        result = result.filter((item) => {
          if (value == 1) {
            return Number(item.outstanding_wages || 0) <= 0;
          } else if (value == 2) {
            return Number(item.outstanding_wages || 0) > 0;
          }
        });
      } else if (key == "item_type") {
        result = result.filter((item) => {
          return item.item_type == value;
        });
      } else {
        result = result.filter((item) => {
          return item[key] == value;
        });
      }
    }
  });
  // 分页
  let total = result.length;
  let totalList = result;
  let list;
  list = result.slice(start, end);
  return {
    total,
    list,
    totalList,
  };
};

export default {
  searchTable,
};
